/**
 * Created by guoqs on 2018/5/14.
 */
import smartCourseApi from 'api/smartCourse'

// state
let state = {
  studentList: [],
  lastScore: {},
  classList: [],
  unreadCount: 0,
  scoreRadar: {},
  bodyDataList: [],
  bodyDataEchart: [],
  curStudent: {},
  abilityGrow: [],
  ageDicts: [],
  lastScoreGroup: [],
  standard: {},
  scoreLineEcharts: {},
  studentScore: {},
  scoreDetail: {},
  scoreRuleInfo: {},
  abilityScoreChart: {},
  abilityScoreList: {},
  teacherList: []
}

// actions
const actions = {
  classTeacherInfo ({ commit, state }, params) {
    return smartCourseApi.classTeacherInfo(params).then(json => {
      commit('TEACHER_LIST', {
        params: params,
        data: json.data
      })
      return json.data
    })
  },
  studentTestAbilityScoreListByPage ({ commit, state }, params) {
    return smartCourseApi.studentTestAbilityScoreListByPage(params).then(json => {
      commit('ABILITY_SCORE_LIST', {
        params: params,
        data: json.data
      })
      return json.data
    })
  },
  studentTestAbilityScoreChartByPage ({ commit, state }, params) {
    return smartCourseApi.studentTestAbilityScoreChartByPage(params).then(json => {
      commit('ABILITY_SCORE_CHART', {
        params: params,
        data: json.data
      })
      return json.data
    })
  },
  itemScoreRuleInfo ({ commit, state }, params) {
    return smartCourseApi.itemScoreRuleInfo(params).then(json => {
      commit('SCORE_RULE_INFO', {
        params: params,
        data: json.data
      })
    })
  },
  studentTestScoreDetail ({ commit, state }, params) {
    return smartCourseApi.studentTestScoreDetail(params).then(json => {
      commit('SCORE_DETAIL', {
        params: params,
        data: json.data
      })
    })
  },
  studentInfo ({ commit, state }, params) {
    return smartCourseApi.studentInfo(params).then((json) => {
      commit('STUDENT_INFO', {
        params: params,
        data: json.data
      })
    })
  },
  findStudent ({ commit, state }, params) {
    return smartCourseApi.findStudent(params).then((json) => {
      commit('FIND_STUDENT', {
        params: params,
        data: json.data
      })
    })
  },
  getLastScore ({ commit, state }, params) {
    return smartCourseApi.getLastScore(params).then((json) => {
      commit('LAST_SCORE', {
        params: params,
        data: json.data
      })
    })
  },
  findClassRoomByStudId ({ commit, state }, params) {
    return smartCourseApi.findClassRoomByStudId(params).then((json) => {
      commit('CLASS_BY_STUDENTID', {
        params: params,
        data: json.data
      })
    })
  },
  findUnreadCount ({ commit, state }, params) {
    return smartCourseApi.findUnreadCount(params).then((json) => {
      commit('UNREAD_COUNT', {
        params: params,
        data: json.data
      })
    })
  },
  studentTestScoreRadarChart ({ commit, state }, params) {
    return smartCourseApi.studentTestScoreRadarChart(params).then((json) => {
      commit('SCORE_RADAR_CHARTS', {
        params: params,
        data: json.data
      })
      return json.data
    })
  },
  studentBodyDataLatest ({ commit, state }, params) {
    return smartCourseApi.studentBodyDataLatest(params).then((json) => {
      if (json.data && json.data.length > 0) {
        const bodyDataList = json.data.filter(item => {
          return item.bodyData && (item.bodyData.dataValue !== undefined)
        })
        commit('BODY_DATA_LATEST', {
          params: params,
          data: bodyDataList
        })
        return bodyDataList
      }
    })
  },
  studentBodyDataEchart ({ commit, state }, params) {
    return smartCourseApi.studentBodyDataEchart(params).then((json) => {
      commit('BODY_DATA_ECHART', {
        params: params,
        data: json.data
      })
      return json
    })
  },
  getStudentLastAbilityScore ({ commit, state }, params) {
    return smartCourseApi.getStudentLastAbilityScore(params).then((json) => {
      commit('ABILITY_GROW', {
        params: params,
        data: json.data
      })
    })
  },
  getAgeDicts ({ commit }) {
    return smartCourseApi.getAgeDicts().then((json) => {
      commit('AGE_DICYS', {
        data: json.data
      })
    })
  },
  getLastScoreGroup ({ commit, state }, params) {
    return smartCourseApi.getLastScoreGroup(params).then(json => {
      commit('LAST_SCORE_GROUP', {
        params: params,
        data: json.data
      })
    })
  },
  standard ({ commit, state }, params) {
    return smartCourseApi.standard(params).then(json => {
      commit('STANDARD', {
        params: params,
        data: json.data
      })
    })
  },
  studentTestScoreLineChart ({ commit, state }, params) {
    return smartCourseApi.studentTestScoreLineChart(params).then(json => {
      commit('SCORE_LINE_ECHARTS', {
        params: params,
        data: json.data
      })
    })
  },
  getStudentScoreByMethodId ({ commit, state }, params) {
    return smartCourseApi.getStudentScoreByMethodId(params).then(json => {
      commit('STUDENT_SCORE', {
        params: params,
        data: json.data
      })
    })
  }
}

// mutations
const mutations = {
  SCORE_DETAIL (state, { data }) {
    state.scoreDetail = data
  },
  STANDARD (state, { data }) {
    state.standard = data
  },
  FIND_STUDENT (state, { data }) {
    state.studentList = data
  },
  STUDENT_INFO (state, { data }) {
    state.curStudent = data
  },
  LAST_SCORE (state, { data }) {
    state.lastScore = data
  },
  CLASS_BY_STUDENTID (state, { data }) {
    state.classList = data
  },
  UNREAD_COUNT (state, { data }) {
    state.unreadCount = data
  },
  SCORE_RADAR_CHARTS (state, { data }) {
    state.scoreRadar = data
  },
  BODY_DATA_LATEST (state, { data }) {
    state.bodyDataList = data
  },
  BODY_DATA_ECHART (state, { data }) {
    state.bodyDataEchart = data
  },
  ABILITY_GROW (state, { data }) {
    state.abilityGrow = data
  },
  AGE_DICYS (state, { data }) {
    state.ageDicts = data
  },
  LAST_SCORE_GROUP (state, { data }) {
    state.lastScoreGroup = data
  },
  SCORE_LINE_ECHARTS (state, { data }) {
    state.scoreLineEcharts = data
  },
  STUDENT_SCORE (state, { data }) {
    state.studentScore = data
  },
  SCORE_RULE_INFO (state, { data }) {
    state.scoreRuleInfo = data
  },
  ABILITY_SCORE_CHART (state, { data }) {
    state.abilityScoreChart = data
  },
  ABILITY_SCORE_LIST (state, { data }) {
    state.abilityScoreList = data
  },
  TEACHER_LIST (state, { data }) {
    state.teacherList = data
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
